<template> <!-- vue实例外创建 -->
  <div>
    <el-breadcrumb separator="/" class="xie">
  <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
  <el-breadcrumb-item><a href="/">{{title}}</a></el-breadcrumb-item>
</el-breadcrumb>
    <template>
      <el-card class="box-card">
        <el-input placeholder="请输入内容" v-model="page.query" class="input-with-select">
          <el-button slot="append" icon="el-icon-search" @click="getdata(1)"></el-button>
        </el-input>
        <el-button @click="drawer = true" type="primary" style="margin-left: 16px;">
          添加
        </el-button>
        <el-table :data="tableData" stripe style="width: 100%">
          <el-table-column type="index" label="#">
          </el-table-column>
          <el-table-column label="日期">
            <template slot-scope="scope">
              <span>{{ (scope.row.create_time * 1000) | newdate }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="username" label="用户名">
          </el-table-column>
          <el-table-column prop="email" label="邮箱">
          </el-table-column>
          <el-table-column prop="mobile" label="电话">
          </el-table-column>
          <el-table-column prop="role_name" label="角色名称"></el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
              <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
            </template>

          </el-table-column>
        </el-table>
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
          :page-sizes="[5, 10, 20, 50]" :current-page="page.pagenum" :page-size="page.pagesize"
          layout="total, sizes, prev, pager, next, jumper" :total="total">
        </el-pagination>
      </el-card>
      <!-- 添加窗口 -->
      <el-drawer title="添加用户" :visible.sync="drawer" :direction="direction" :before-close="handleClose">
        <el-form :rules="rules" ref="userref" :model="userform" label-width="80px">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="userform.username"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="userform.password"></el-input>
          </el-form-item>
          <el-form-item label="邮箱">
            <el-input v-model="userform.email"></el-input>
          </el-form-item>
          <el-form-item label="手机号">
            <el-input v-model="userform.mobile"></el-input>
          </el-form-item>
          <el-button class="add" type="primary" @click="adduserfun">确认添加</el-button>
        </el-form>
      </el-drawer>
      <!-- 编辑 -->
      <el-dialog title="修改用户信息" :visible.sync="dialogVisible" width="50%" :before-close="handleClose">
        <el-form :rules="rules" ref="userref" :model="userform" label-width="80px">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="userform.username" disabled></el-input>
          </el-form-item>
          <el-form-item label="邮箱">
            <el-input v-model="userform.email"></el-input>
          </el-form-item>
          <el-form-item label="手机号">
            <el-input v-model="userform.mobile"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="edituserfun">确 定</el-button>
        </span>
      </el-dialog>
    </template>
  </div>
</template>
<script>
// 接口
import { GetUsers, AddUsers, GetOneUsers, EditUsers, DelOenUsers } from '@/api/user'
export default {
  data() {
    return {
      //编辑按钮的弹窗
      dialogVisible: false,

      drawer: false,
      direction: 'rtl',
      tableData: [],
      userform: {},
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'change' },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
        ]
      },
      //传参
      page: {
        pagenum: 1,
        pagesize: 5,
        query: ''
      },
      //总条数
      total: 0,
      router: true,
      title:''
    }
  },
  methods: {
    //修改
    async handleEdit(index, { id }) {
      this.dialogVisible = true
      console.log(id);
      let { data, meta } = await GetOneUsers(id)
      if (meta.status != 200) return this.$message.error(meta.msg)
      console.log(meta);
      this.userform = data
    },
    //确认修改
    async edituserfun() {
      console.log(this.userform);
      let { data, meta } = await EditUsers(this.userform)
      if (meta.status != 200) return this.$message.error(meta.msg);
      this.$message.success(meta.msg);
      //1.关闭窗口
      this.dialogVisible = false
      //2.更新页面
      this.getdata()
      //3.清空之前的旧数据
      this.userform = {}

    },
    //   删除
    async handleDelete(index, { id }) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        let { data, meta } = await DelOenUsers(id)
        if (meta.status != 200) return this.$message.error(meta.msg);
        this.$message.success(meta.msg);
        this.getdata()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });

    },


    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done();
        })
        .catch(_ => { });
    },
    //分页器
    handleSizeChange(val) {
      this.page.pagesize = val
      this.page.pagenum = 1
      this.getdata()
    },
    handleCurrentChange(val) {
      this.page.pagenum = val
      this.getdata()
    },
    //确认添加
    adduserfun() {
      this.$refs['userref'].validate(async v => {
        if (v) {
          console.log(this.userform);
          let { data, meta } = await AddUsers(this.userform)
          if (meta.status != 201) return this.$message.error(meta.msg);
          this.$message.success(meta.msg);
          //1.关闭窗口
          this.drawer = false
          //2.更新页面
          this.getdata()
          //3.清空之前的旧数据
          this.userform = {}
        }
      })
    },

    //获取数据
    async getdata(e) {
      if (e == 1) {
        this.page.pagenum = 1
      }
      let { data, meta } = await GetUsers(this.page)
      if (meta.status != 200) return this.$message.error(meta.msg);
      this.tableData = data.users
      console.log(this.tableData);
      this.total = data.total
    },
  },
  mounted() {
    this.getdata()
    this.$bus.$on('getTitle',(e)=>{
      this.title=e
    })
    this.title = localStorage.getItem('title')
  }
}

</script>
<style>
.add {
  text-align: center;
}

.input-with-select {
  width: 60%;
}
.xie{
  margin-bottom: 10px;
}
</style>